package cn.anxingongyou.anxin.service.impl;

import cn.anxingongyou.anxin.entity.CommentEmployer;
import cn.anxingongyou.anxin.mapper.CommentEmployerMapper;
import cn.anxingongyou.anxin.service.CommentEmployerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class CommentEmployerServiceImpl implements CommentEmployerService {

    private final CommentEmployerMapper commentEmployerMapper;
    @Autowired
    public CommentEmployerServiceImpl(CommentEmployerMapper commentEmployerMapper) {
        this.commentEmployerMapper = commentEmployerMapper;
    }

    @Override
    public int insert(CommentEmployer commentEmployer) {
        return commentEmployerMapper.insertSelective(commentEmployer);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return commentEmployerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(CommentEmployer commentEmployer) {
        return commentEmployerMapper.updateByPrimaryKeySelective(commentEmployer);
    }

    @Override
    public CommentEmployer selectOne(CommentEmployer commentEmployer) {
        return commentEmployerMapper.selectOne(commentEmployer);
    }

    @Override
    public CommentEmployer selectOneByPrimaryKey(Long id) {
        return commentEmployerMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<CommentEmployer> page(int pageNum, int pageSize, CommentEmployer commentEmployer) {
        String orderBy = "id desc";
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageNum,pageSize,orderBy);
        List<CommentEmployer> list = commentEmployerMapper.select(commentEmployer);
        PageInfo<CommentEmployer> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<CommentEmployer> search(int pageNum, int pageSize, CommentEmployer commentEmployer) {
        String orderBy = "id desc";
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageNum,pageSize,orderBy);

        Example example = new Example(CommentEmployer.class);
        Example.Criteria criteria = example.createCriteria();
        if(commentEmployer.getId() != null){
            criteria.andEqualTo("id",commentEmployer.getId());
        }
        if(commentEmployer.getEmployerId() != null){
            criteria.andEqualTo("employerId",commentEmployer.getEmployerId());
        }
        if(commentEmployer.getCommentatorId() != null){
            criteria.andEqualTo("commentatorId",commentEmployer.getCommentatorId());
        }
        if(commentEmployer.getName() != null){
            criteria.andLike("name","%" + commentEmployer.getName()+ "%");
        }
        if(commentEmployer.getContent() != null){
            criteria.andLike("content","%" +  commentEmployer.getContent() + "%");
        }
        List<CommentEmployer> list = commentEmployerMapper.selectByExample(example);
        PageInfo<CommentEmployer> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
